package com.vcredit.creditcat.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.vcredit.creditcat.framework.pagination.DefaultCriteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.framework.util.HttpClientUtil;
import com.vcredit.creditcat.model.ChannelStatisticsExternal;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.RegisterChannel;
import com.vcredit.creditcat.persistence.base.RegisterChannelMapper;
import com.vcredit.creditcat.service.ChannelStatisticsExternalService;
import com.vcredit.creditcat.service.CountService;
import com.vcredit.creditcat.service.ExportService;
import com.vcredit.creditcat.service.MerchantProductService;
import com.vcredit.creditcat.service.ResourceService;
import com.vcredit.creditcat.vo.ChanneManVo;
import com.vcredit.framework.util.JacksonUtil;

/**
 * 
 * @author chengxin
 *
 */
@RestController
@RequestMapping("/count")
public class CountController extends BaseController {

	@Value("${api.url}")
	private String apiUrl;

	@Autowired
	private ExportService<ChanneManVo> exportService;
	@Autowired
	private CountService countService;
	@Autowired
	private ChannelStatisticsExternalService channelStatisticsExternalService;

	@Autowired
	private RegisterChannelMapper registerChannelMapper;
	@RequestMapping("/list")
	public String list() {
		return "count/list";
	}

	@RequestMapping("/channeList")
	public ModelAndView showPageFeedback(HttpServletRequest request) {
		return new ModelAndView("/count/channelList");
	}

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private MerchantProductService merchantProductService;

	/**
	 * 根据当前权限查询 当前的统计
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/channelPage")
	public ModelAndView channeList(HttpServletRequest request) {
		return new ModelAndView("/channe/channeStatisticsList");
	}

	@RequestMapping("/queryStatic")
	@ResponseBody
	public String queryStatic(String createTimeStart, String createTimeEnd, String fbType) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("createTimeStart", createTimeStart);
		map.put("createTimeEnd", createTimeEnd);
		map.put("fbType", fbType);
		String result = HttpClientUtil.doPostByJson(apiUrl + "count/getStatisticsInfo", map);
		return result;
	}

	@RequestMapping("/renewList")
	public String renewList() {
		return "count/renewList";
	}

	@RequestMapping("/queryRepeat")
	@ResponseBody
	public String queryRepeat(String createTimeStart, String createTimeEnd, String fbType) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("createTimeStart", createTimeStart);
		map.put("createTimeEnd", createTimeEnd);
		map.put("fbType", fbType);
		String result = HttpClientUtil.doPostByJson(apiUrl + "count/getRepeatDetail", map);
		return result;
	}

	@RequestMapping("/saveChannelStatisticsExternal")
	public String saveChannelStatisticsExternal(@RequestBody ChannelStatisticsExternal bannerVo,
			HttpServletRequest request) {
		channelStatisticsExternalService.insert(bannerVo);
		return SUCCESS;
	}

	@RequestMapping("/updateChannelStatisticsExternal")
	public String updateChannelStatisticsExternal(@RequestBody ChannelStatisticsExternal bannerVo,
			HttpServletRequest request) {
		// 渠道数据查询权限
		if (null == bannerVo.getChannelId()) {
			RegisterChannel registerChannel = new RegisterChannel();
			registerChannel.setChannelDesc(bannerVo.getChannelName());
			List<RegisterChannel> chanelName = registerChannelMapper.find(registerChannel);
			if (chanelName.size() > 0) {
				bannerVo.setChannel(chanelName.get(0).getChannel());
			}
			channelStatisticsExternalService.insert(bannerVo);
		} else {
			channelStatisticsExternalService.update(bannerVo);
		}

		return SUCCESS;
	}

	@RequestMapping("/listchannelPages")
	public Page<ChannelStatisticsExternal> listchannelPages(HttpServletRequest request) {
		Page<ChannelStatisticsExternal> channeManVo = null;
		try {
			// 渠道
			List<MerchantProduct> channelData = merchantProductService.findAll();

			// List<Resource> channelData =
			// resourceService.findByUserId(SecurityHelper.getUserId(), 2);
			if (channelData == null || channelData.size() < 1) {
				return null;
			}
			String[] channelArray = new String[channelData.size()];
			for (int i = 0; i < channelData.size(); i++) {
				channelArray[i] = channelData.get(i).getProductName();
			}
			DefaultCriteria defaultCriteria = makeDefaultCriteria(request);
			Map<String, Object> params = defaultCriteria.getParams();
			params.put("channelArray", channelArray);
			channeManVo = countService.findByPages(defaultCriteria);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return channeManVo;
	}

	@RequestMapping("/listchanne")
	public List<ChanneManVo> listchanne(HttpServletRequest request) {
		return countService.findByTime(request, "");

	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/listchanneExport")
	public String listchanneExport(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		String resultMap = "";
		try {
			List<ChanneManVo> chioList = countService.findByTime(request, "");
			if (chioList.size() > 0) {
				List<ChanneManVo> chioListGroup = countService.findByTime(request, "groupByTime");
				map = exportService.exportExcel(request, response, "channe", chioList, chioListGroup);
				resultMap = JacksonUtil.Obj2Json(map);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return resultMap;
	}

	@ResponseBody
	@RequestMapping("/downExcel")
	public void downExcel(HttpServletRequest request, HttpServletResponse response, String fileName) {
		try {
			exportService.downExcel(response, fileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/removeChannelStatisticsExternal")
	public String removeFeedback(@RequestParam("ids") String ids, HttpServletRequest request) {
		channelStatisticsExternalService.delete(ids);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/listchannelPage")
	public String listchannelPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		String resultMap = "";
		try {
			List<ChanneManVo> chioList = countService.findByTime(request, "");
			if (chioList.size() > 0) {
				List<ChanneManVo> chioListGroup = countService.findByTime(request, "groupByTime");
				map = exportService.exportExcel(request, response, "channe", chioList, chioListGroup);
				resultMap = JacksonUtil.Obj2Json(map);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 返回所有产品集合
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/listMarchinProduct")
	public String listMarchinProduct(HttpServletRequest request) {
		List<MerchantProduct> merchantProduct = new ArrayList<MerchantProduct>();
		String resultMap = "";
		try {
			// 渠道数据查询权限
			merchantProduct = merchantProductService.findAll();
			resultMap = JacksonUtil.Obj2Json(merchantProduct);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultMap;
	}
}
